package cn.nawang.ebeim.api.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.StorageServer;
import cn.nawang.ebeim.api.repository.StorageServerRepository;
import cn.nawang.ebeim.api.repository.impl.StorageServerRepositoryImpl;
import cn.nawang.ebeim.api.service.StorageServerService;

/**
 * @ClassName: StorageServerServiceImpl
 * @Description: TODO
 * @author fanyb
 * @date Aug 18, 2015 11:35:29 AM
 * @version v1.0
 */
@Service("storageServerService")
public class StorageServerServiceImpl implements StorageServerService{

  @Autowired
  private StorageServerRepository storageServerRepository;
  
  @Autowired
  private StorageServerRepositoryImpl storageServerRepositoryImpl;

  @Override
  public List<StorageServer> findByCloudIdList(String storageCloudId) {
    return storageServerRepository.findByCloudIdList(storageCloudId);
  }

  @Override
  public List<StorageServer> findAll() {
    return storageServerRepository.findAll();
  }

  @Override
  public boolean add(StorageServer storageServer) {
    try {
      storageServer.setId("SS-" + new Date().getTime() + "-" + new Random().nextInt(1000));
      storageServerRepository.save(storageServer);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public PageList findPageList(Pages page) {
    return storageServerRepositoryImpl.findAllByPaging(page);
  }

  @Override
  public StorageServer findOne(String id) {
    return storageServerRepository.findOne(id);
  }

  @Override
  public boolean delete(String[] ids) {
    return storageServerRepositoryImpl.delete(ids);
  }

  @Override
  public boolean update(StorageServer storageServer) {
    try {
      storageServerRepository.save(storageServer);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  @Override
  public StorageServer findByIpAndPort(String host, int port) {
    return storageServerRepositoryImpl.findByIpAndPort(host, port);
  }
}
